home *** CD-ROM | disk | FTP | other *** search
Wrap
// HQHackMac.m // // You may freely copy, distribute, and reuse the code in this example. // Apple disclaims any warranty of any kind, expressed or implied, as to its // fitness for any particular use. #import "HQHackMac.h" #import "HQInfoPanelController.h" #import "HQPreferencesController.h" // If you want the bundle to insert a menu into apps that load it, uncomment the next line. Otherwise, comment it out. #define ENABLE_MENU @implementation HQHackMac // ************************* Set up ************************* + (void)load { #ifdef ENABLE_MENU // Arrange to install the menu. We will try at least three different ways of getting notified. // We try to install the menu at willFinishLaunching, but we will try again at didFinishLaunching just in case willFinishLaunching passed us by. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationWillFinishLaunchingNotification object:NSApp]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryToInstallMenu:) name:NSApplicationDidFinishLaunchingNotification object:NSApp]; // Finally, there may be cases where the didFinishLaunching has already happened by the time we get loaded. So we'll also try it with a timer too. [self performSelector:@selector(tryToInstallMenu:) withObject:nil afterDelay:0.0]; #endif } // ********************** Install extras menu ********************** + (void)tryToInstallMenu:(NSNotification *)notification { static BOOL alreadyInstalled = NO; NSMenu *mainMenu = nil; // We only try once, but that one time needs to be after there is a main menu. If there is none now, we will pass and hope this method gets called again later. if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) { NSMenu *insertIntoMenu = nil; NSMenuItem *item; unsigned insertLoc = NSNotFound; NSBundle *bundle = [NSBundle bundleForClass:[HQHackMac class]]; // Succeed or fail, we do not try again. alreadyInstalled = YES; item = (([mainMenu numberOfItems] > 0) ? [mainMenu itemAtIndex:0] : nil); if (item && [item hasSubmenu]) { insertIntoMenu = [item submenu]; item = [insertIntoMenu itemWithTitle:NSLocalizedStringFromTableInBundle(@"Services", @"Localizable", bundle, @"Title of Services menu item")]; if (!item) { // MF: Try again with unlocalized string "Text". This is a backstop that assumes that any app not localized into the user's chosen language was probably developed in English... item = [insertIntoMenu itemWithTitle:@"Services"]; } if (item) { insertLoc = [[insertIntoMenu itemArray] indexOfObjectIdenticalTo:item] + 1; } else { // Just insert at head of menu. This is kind of yucky... insertLoc = 0; } } if (insertIntoMenu) { NSMenu *hackMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:NSLocalizedStringFromTableInBundle(@"HackMac", @"Localizable", bundle, @"Title of HackMac menu")]; item = [insertIntoMenu insertItemWithTitle:NSLocalizedStringFromTableInBundle(@"HackMac", @"Localizable", bundle, @"Title of HackMac menu") action:NULL keyEquivalent:@"" atIndex:insertLoc]; [insertIntoMenu setSubmenu:hackMenu forItem:item]; // Add the items for the commands. item = [hackMenu addItemWithTitle:NSLocalizedStringFromTableInBundle(@"About HackMac", @"Localizable", bundle, @"Title of Info Panel menu item") action:@selector(showWindow:) keyEquivalent:@""]; [item setTarget:[HQInfoPanelController sharedInfoPanelController]]; #ifdef ENABLE_PREFERENCES item = [hackMenu addItemWithTitle:NSLocalizedStringFromTableInBundle(@"Preferences...", @"Localizable", bundle, @"Title of Preferences Panel menu item") action:@selector(showWindow:) keyEquivalent:@""]; [item setTarget:[HQPreferencesController sharedPreferencesController]]; #endif } } } @end